var mygrid ;
$(function() {
	mygrid = $("#list").jqGrid({
		url : 'fileflow/group/custom-organization!datalist',
		datatype : "json",
		mtype : 'GET',
		colNames : [ '名称', '用户', '删除' ],
		colModel : [ {
			name : 'name',
			index : 'name',
			width : 210,
			formatter : nameFormater
		},{
			name : 'users',
			index : 'users',
			width : 50,
			formatter : userFormatter
		}, {
			name : 'id',
			index : 'id',
			width : 50,
			formatter : removeFormatter
		}, ],
		pager : '#pager',
		rowNum : 10,
		rowList : [ 10, 20, 30 ],
		sortname : 'name',
		sortorder : 'desc',
		viewrecords : true,
		caption : '临时部门',
		width : '500',
		height : '300',
		jsonReader : {
			repeatitems : false,
			id : "0"
		}
	});
	mygrid.jqGrid("navGrid","#pager",{edit:false,add:false,del:false,search:false});
	mygrid.jqGrid("navButtonAdd","#pager",{
	    onClickButton:function(){
	    	 doAjaxLoad("fileflow/group/start");
	    },
	    buttonicon:"ui-icon-newwin",
	    caption:"添加新组",
	    id:"newGroup"
	});	
	
	function nameFormater(cellvalue, options, rowObject) {
		return cellvalue;

	}	

	function userFormatter(cellvalue, options, rowObject) {
		if(cellvalue==null || cellvalue.length==0)return "-";
		var sel=$("<select></select>")
		
		$.each(cellvalue,function(idx,item){
			var opt = $("<option></option>");
			opt.val(item.username);
			opt.text(item.localName);
			sel.append(opt);
		});
		
		return $("<p></p>").append(sel).html();
	}
	
	function removeFormatter(val, opt, row) {
		var link=$("<a style='color:red' href=\"javascript:removeGroup('"+val+"');\"></a>").text("删除");
		return $("<p></p>").append(link).html();
	}

});
function removeGroup(id){
	$("#jqgrid").block({"message":"正在处理..."});
	$.ajax({
		url:"fileflow/group/custom-organization!remove",
		data:"bean.id="+id,
		success:function(){
			$("#jqgrid").unblock();
			mygrid.trigger("reloadGrid");
		}
	});
}
function addNewCustorg(url, eid) {
	doAjaxLoad("fileflow/group/start");
}